import storesApi from "../api/stores"
import distributionApi from "../api/distribution"

export default {
    namespaced: true,
    state: {
        curpage: 1,
        eachPage: 3,
        total: 0,
        maxPage: 0,
        rows: [],
        storesrows: [],
    },
    mutations: {
        get(state, data) {
            Object.assign(state, data)
            console.log(state);
        },
        getstores(state, data) {
            state.storesrows = data.rows
        },
        queryList(state, data) {
            state.rows = data
        },
        setCurPage(state, curPage) {
            console.log();
            state.curpage = curPage;
        },
    },
    actions: {
        addDistribution: async (state, data) => {
            const { name, phone, address, date, storeId, busy } = data
            const newdistribution = await distributionApi.post(name, phone, address, date, storeId, busy)
            return newdistribution
        },
        getstores: async ({ state, commit }) => {
            const { curpage, eachPage } = state;
            const data = await storesApi.get(curpage, eachPage);
            commit("getstores", data)
        },
        get: async ({ state, commit }) => {
            const { curpage, eachPage } = state;
            const data = await distributionApi.get(curpage, eachPage);
            commit("get", data)
        },
        update: async (state, { id, phone, address }) => {
            const updatedistribution = await distributionApi.update(id, phone, address)
            return updatedistribution
        },
        searchDistribution: async ({ commit }, { searchName }) => {
            const data = await distributionApi.searchDistribution(searchName);
            commit("queryList", data);
        },
        delete: async (state, { decid }) => {
            const deletedistribution = await distributionApi.delete(decid)
            return deletedistribution
        },
        
    }
}
